package logic

import (
	"context"
	"errors"
	"meituan_srv/model/mysql"
	"meituan_srv/model/redisinit"

	"meituan_srv/internal/svc"
	"meituan_srv/meituan"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCartAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartAddLogic {
	return &CartAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 购物车添加
func (l *CartAddLogic) CartAdd(in *meituan.CartAddRequest) (*meituan.CartAddResponse, error) {
	// todo: add your logic here and delete this line

	m := mysql.MeiTuan{}
	err := m.Info(in.MeiTuanId)
	if err != nil {
		logx.Error("商品不存在")
		return nil, errors.New("商品不存在")
	}
	if m.ID == 0 {
		logx.Error("商品不存在")
		return nil, errors.New("商品不存在")
	}
	if int64(m.Num) < in.Num {
		logx.Error("商品库存不够")
		return nil, errors.New("商品库存不够")
	}
	redisinit.CartAdd(m.Cover, m.Name, m.Status, in.Num, in.UserId, in.MeiTuanId, m.Price)
	return &meituan.CartAddResponse{}, nil
}
